package com.rasenhund.android.gsimulator.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rasenhund.android.gsimulator.R;
import com.rasenhund.android.gsimulator.common.Const;
import com.rasenhund.android.gsimulator.schema.MobileSuit;

/**
 * 編成選択画面
 */
public class SelectSlotActivity extends BaseActivity {
    @SuppressWarnings("unused")
    private static final String TAG = SelectSlotActivity.class.getSimpleName();
    private final SelectSlotActivity self = this;

    TextView mTxtMsCost;
    TextView mTxtBaseCost;
    TextView mTxtTotalCost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select_slot);

        // 機体一覧
        for (int i = 0; i < mSlotIdList.length; i++) {

            RelativeLayout slot = (RelativeLayout) findViewById(mSlotIdList[i]);
            slot.setClickable(true);
            slot.setOnClickListener(slot_OnClickListener);
            slot.setOnLongClickListener(slot_OnLongClickListener);

            // 機体No受け渡し用にIndexを隠し項目に保存
            TextView hidRowNo = (TextView) slot.findViewById(R.id.hidRowNo);
            hidRowNo.setText(String.valueOf(i));

            // viewにアイテムをセット
            setSlot(slot, mLstMachine.get(i));

        }

        // 確定ボタン
        Button btnConfirm = (Button) findViewById(R.id.btnConfirm);
        btnConfirm.setOnClickListener(btnConfirm_OnliClickListener);

        mTxtMsCost = (TextView) findViewById(R.id.txtMsCost);
        mTxtBaseCost = (TextView) findViewById(R.id.txtBaseCost);
        mTxtTotalCost = (TextView) findViewById(R.id.txtTotalCost);

    }

    @Override
    protected void onResume() {
        super.onResume();

        calcTotalCost();

    }

    /********************
     * EventListener
     ********************/

    /**
     * MSスロットクリック時のイベントリスナ
     */
    private OnClickListener slot_OnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            // 隠し項目から機体番号を取得
            TextView txtRowNo = (TextView) v.findViewById(R.id.hidRowNo);
            mMachineNo = Integer.valueOf(txtRowNo.getText().toString());

            Intent intent = new Intent(self, SelectCategoryActivity.class);
            startActivity(intent);

        }
    };

    /**
     * MSスロット長押し時のイベントリスナ
     */
    private OnLongClickListener slot_OnLongClickListener = new OnLongClickListener() {

        @Override
        public boolean onLongClick(View view) {

            // 隠し項目から機体番号を取得
            TextView hidRowNo = (TextView) view.findViewById(R.id.hidRowNo);
            int iRowNo = Integer.valueOf(hidRowNo.getText().toString());

            // MSスロットのクリア
            MobileSuit item = new MobileSuit();
            mLstMachine.set(iRowNo, item);

            // 空データを再セット
            setSlot(view, item);

            // コスト再計算
            calcTotalCost();

            return true;
        }
    };

    /**
     * 確定ボタン押下時のイベントリスナ
     */
    private OnClickListener btnConfirm_OnliClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            // コストシミュレート画面に遷移
            Intent intent = new Intent(self, CostSimulateActivity.class);
            startActivity(intent);

        }
    };

    /********************
     * Method
     ********************/
    /**
     * 合計コストの計算、表示
     */
    private void calcTotalCost() {

        int iMsCostTotal = 0;// MSコスト

        int iVsCount = 0; // 対戦人数

        RelativeLayout slot = null;
        TextView hidCost = null;
        int iMsCost = 0;

        // MSコストの計算
        for (int i = 0; i < mSlotIdList.length; i++) {

            slot = (RelativeLayout) findViewById(mSlotIdList[i]);
            hidCost = (TextView) slot.findViewById(R.id.hidCost);

            iMsCost = Integer.valueOf(hidCost.getText().toString());
            iMsCostTotal = iMsCostTotal + iMsCost;

            // 総コスト計算用に件数を計算
            if (iMsCost != 0) {
                iVsCount++;
            }

        }

        // 総コストの取得
        if (iVsCount <= 4) {
            mTotalCost = 2000;
        } else {
            mTotalCost = 500 * iVsCount;
        }

        // 拠点コストの取得(総コストの25%)
        mBaseCost = (int) (mTotalCost * 0.25);

        mTxtMsCost.setText(String.valueOf(iMsCostTotal));
        mTxtBaseCost.setText(String.valueOf(mBaseCost));
        mTxtTotalCost.setText(String.valueOf(mTotalCost));

    }

    /**
     * Viewにアイテムをセット
     * 
     * @param view
     * @param item
     */
    private void setSlot(View view, MobileSuit item) {

        TextView txtCategory = (TextView) view.findViewById(R.id.txtCategory);
        TextView txtModelNo = (TextView) view.findViewById(R.id.txtModelNo);
        TextView txtMsName = (TextView) view.findViewById(R.id.txtMsName);
        TextView txtCost = (TextView) view.findViewById(R.id.txtCost);
        TextView hidCost = (TextView) view.findViewById(R.id.hidCost);

        switch (item.getCategoryCd()) {
            case Const.CATEGORY.GRAPP:
                txtCategory.setText(getString(R.string.category_grapp));
                txtCategory.setBackgroundResource(R.drawable.category_grapp);
                break;
            case Const.CATEGORY.SHORT:
                txtCategory.setText(getString(R.string.category_short));
                txtCategory.setBackgroundResource(R.drawable.category_short);
                break;
            case Const.CATEGORY.SHOOT:
                txtCategory.setText(getString(R.string.category_shoot));
                txtCategory.setBackgroundResource(R.drawable.category_shoot);
                break;
            case Const.CATEGORY.TANK:
                txtCategory.setText(getString(R.string.category_tank));
                txtCategory.setBackgroundResource(R.drawable.category_tank);
                break;
            case Const.CATEGORY.SNIPE:
                txtCategory.setText(getString(R.string.category_snipe));
                txtCategory.setBackgroundResource(R.drawable.category_snipe);
                break;
            default:
                txtCategory.setText("-");
                txtCategory.setBackgroundResource(R.drawable.normal);
                break;
        }

        txtModelNo.setText(item.getModelNo());
        txtMsName.setText(item.getMsNameShort(12));
        txtCost.setText(String.valueOf(item.getCost()));
        hidCost.setText(String.valueOf(item.getCost()));

    }

}
